/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "ButtonBar.h"

/**
 * \class ButtonBar
 *
 * \short Contains all buttons of the Gui
 *
 * All buttons
 */

/**
 * \short Constructor of the ButtonBar class
 *
 * Initialise all buttons
 */

ButtonBar::ButtonBar() {
 // Initialize the layout
  layout = new QHBoxLayout;

  // The combobox for the channels.
  teleTextBox = new QComboBox();

  // Prev button
  prevButton = new QPushButton(this);
  prevButton->setText("<-");
  prevButton->setShortcut(QObject::tr(BUTTON_PREV));

  // Next button
  nextButton = new QPushButton(this);
  nextButton->setText("->");
  nextButton->setShortcut(QObject::tr(BUTTON_NEXT));

  // Up button
  upButton = new QPushButton(this);
  upButton->setText("/\\");
  upButton->setShortcut(QObject::tr(BUTTON_UP));

  // Down button
  downButton = new QPushButton(this);
  downButton->setText("\\/");
  downButton->setShortcut(QObject::tr(BUTTON_DOWN));

  // Page setter
  pageid = new QSpinBox(this);
  // 
  pageid->setRange(MIN_PAGE_RANGE, MAX_PAGE_RANGE);
  pageid->setValue(MIN_PAGE_RANGE);


  // Dirty hack to allow a user to press 'space' followed by a page id
  pfocus = new QPushButton(this);
  pfocus->setShortcut(QObject::tr(PAGE_SELECTOR));
  // Also; make sure it's invisible
  pfocus->setVisible(false);

  // Sub page setter
  subpageid = new QSpinBox(this);
  subpageid->setRange(MIN_SUB_PAGE_RANGE, MAX_SUB_PAGE_RANGE);
  subpageid->setValue(MIN_SUB_PAGE_RANGE);

  // Quit button
  quitButton = new QPushButton(this);
  quitButton->setText("Quit");
  quitButton->setShortcut(QObject::tr(BUTTON_QUIT));

  // Help button
  helpButton = new QPushButton(this);
  helpButton->setText("Help");
  helpButton->setShortcut(QObject::tr(BUTTON_HELP));

  layout->addWidget(prevButton);
  layout->addWidget(pageid);
  layout->addWidget(nextButton);
  layout->addWidget(upButton);
  layout->addWidget(subpageid);
  layout->addWidget(downButton);
  layout->addWidget(teleTextBox);
  layout->addWidget(helpButton);
  layout->addWidget(quitButton);

  this->setLayout(layout);
}

ButtonBar::~ButtonBar() {
    delete teleTextBox;
    delete layout;
    delete cfocus;
    delete downButton;
    delete helpButton;
    delete nextButton;
    delete pfocus;
    delete prevButton;
    delete quitButton;
    delete upButton;
    delete pageid;
    delete subpageid;
}

/**
 * \short Add a channel to the channel selector
 *
 * \param item The string to add. 
 */

void ButtonBar::addChannel(const QString& item) {
  teleTextBox->addItem(item);
}

/**
 * \short Add a channel to the channel selector
 *
 * \param item The string to add. 
 */

void ButtonBar::setChannel(const QString& item) {
  int i = 0;
  while(item != teleTextBox->itemText(i)) {
    i++;
  }
  teleTextBox->setCurrentIndex(i);
}

/**
 * \short Connect all signals to the related slot functions. 
 * 
 * \param ptr A pointer to the Controller class
 * 
 * Connect all signals to the related slot functions. 
 */

void ButtonBar::connectSlots(Controller *ptr) {
  QObject::connect(downButton, SIGNAL(clicked()), ptr, SLOT(downPressed()));
  QObject::connect(helpButton, SIGNAL(clicked()), ptr, SLOT(helpPressed()));
  QObject::connect(nextButton, SIGNAL(clicked()), ptr, SLOT(nextPressed()));
  QObject::connect(prevButton, SIGNAL(clicked()), ptr, SLOT(prevPressed()));
  QObject::connect(upButton, SIGNAL(clicked()), ptr, SLOT(upPressed()));

  QObject::connect(subpageid, SIGNAL(valueChanged(int)), ptr, SLOT(teleTextSubPageChanged(int)));
  QObject::connect(pageid, SIGNAL(valueChanged(int)), ptr, SLOT(teleTextPageChanged(int)));
  QObject::connect(teleTextBox, SIGNAL(currentIndexChanged(const QString &)), ptr, SLOT(teleTextChannelChanged(const QString &)));

  // Note: Part of the above hack
  QObject::connect(pfocus, SIGNAL(clicked()), this, SLOT(setPageFocus()));
}

/**
 * \short Connect the quit signal to the quit button
 * 
 * \param app A pointer to the QApplication
 * 
 * Connect the quit signal to the quit button
 */

void ButtonBar::connectQuit(QMainWindow *app) {
  QObject::connect(quitButton, SIGNAL(clicked()), app, SLOT(saveQuit()));
}

/**
 * \short Set focus to the page thingy.
 * 
 * Set focus to the page thingy.
 */

void ButtonBar::setPageFocus() {
  pageid->setFocus();
  // Select all allows a user to start typing immediatly
  pageid->selectAll();
}

/**
 * \short Remove the focus from the page and sub page combo boxes. 
 *
 * Remove the focus from the page and sub page combo boxes. 
 * Useful so the user can keep on using the shortcuts
 */

void ButtonBar::clearFocus() {
  if(pageid->hasFocus()) {
    pageid->clearFocus();
  }
  if(subpageid->hasFocus()) {
    subpageid->clearFocus();
  }
}

/**
 * \short Set the param page as current.
 *
 * \param page The current page to select. 
 *
 * The QComboBox of the page will be set
 */

void ButtonBar::setPage(int page) {
  pageid->setValue(page);
}

/**
 * \short Set the param subpage as current.
 *
 * \param page The current subpage to select. 
 *
 * The QComboBox of the subpage will be set
 */

void ButtonBar::setSubPage(int subpage) {
  subpageid->setValue(subpage);
}
